﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fsxm.LispEdit;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Plugin_Prj;

[assembly: PluginClassAttribute(typeof(PluginMain.PluginMain))]

namespace PluginMain
{

    public class PluginMain : Plugin
    {
        //初始化按钮
        public override void Start()
        {

            ////插入文字
            //AddMenuItem("插件_插入文字",
            //    () =>
            //    {
            //        var edit = ActivateEdit;
            //        if (edit == null) return;
            //        edit.InsertText("插入文字");
            //    });
            //AddMenuItem("插件_对话框",
            //    () =>
            //    { Form frm = new Form(); frm.Show(EditApp); });

            //全部折叠
            var menu_FoldingAll = AddMenuItem("全部折叠", null);
            menu_FoldingAll.Click += FoldingAll_Click;
            menu_FoldingAll.ShortcutKeys = Keys.Control | Keys.D1;
            //全部展开
            var menu_ExpandAll = AddMenuItem("全部展开", null);
            menu_ExpandAll.ShortcutKeys = Keys.Control | Keys.D2;
            menu_ExpandAll.Click += ExpandAll_Click;
            //获取变量的值
            var menu_GetVarValue = AddMenuItem("复制变量或者运算结果值", null);
            menu_GetVarValue.Click += menu_GetVarValue_Click;
            menu_GetVarValue.ShortcutKeys = Keys.F7;

            ProjectManage projectForm = new ProjectManage();
            //项目管理器
            var menu_ShowPrjMana = AddMenuItem("项目管理器", () =>
            {
                if (projectForm.Visible == false)
                {
                    DockPanel panel = EditApp.Controls["dockPanel1"] as DockPanel;
                    ProjectManage frm = new ProjectManage();
                    frm.Show(panel, DockState.DockRight);
                }
                else
                {
                    projectForm.Activate();
                }
            }
            );
            menu_ShowPrjMana.ShortcutKeys = Keys.F8;
            ////记事本
            //var menu_ShowNotpad = AddMenuItem("记事本", () =>
            //    {
            //        DockPanel panel = EditApp.Controls["dockPanel1"] as DockPanel;
            //        Notepad frm = new Notepad();
            //        frm.Show(panel, DockState.Document);
            //        frm.MdiParent = EditApp;
            //        var doc = EditApp.CreateLispDoc();
            //        var edit = doc.FCTB;
            //        frm.groupBox1.Controls.Add(edit);
            //        doc.IsHidden = true;
            //         frm.FormClosed += (a, b) =>
            //        {
            //            doc.Controls.Add(edit);
            //            doc.Close();
            //        };
            //    }
            //    );
        }
        //复制变量的值
        private void menu_GetVarValue_Click(object sender, EventArgs e)
        {
            var edit = ActivateEdit;
            if (edit != null)
            {
                LoadCode("(setClipText (vl-prin1-to-string(eval(read(progn\"" + edit.SelectedText + "\")))))");
            }
        }
        //全部展开
        void FoldingAll_Click(object sender, EventArgs e)
        {
            var edit = ActivateEdit;
            if (edit == null) return;
            edit.CollapseAllFoldingBlocks();

        }
        //全部折叠
        void ExpandAll_Click(object sender, EventArgs e)
        {
            var edit = ActivateEdit;
            if (edit == null) return;

            edit.ExpandAllFoldingBlocks();

        }

    }
    //获取当前选择的文本
    //LoadCode(edit.SelectedText);
    //执行Lisp
    //InvokeLisp("prompt","Test")

    //edit.Doc.LastRead 语法树

    //edit.DoAutoIndent() 格式化行

    //获得所有的文档
    //foreach doc in DocumentManager

    //doc.FCTB.Text 这里取得文档内的文字
    //嗯有filename


    //edit.Selection.Start这个是当前光标位置


    //doc.FindBlock(光标位置,doc.LastRead.Root)  可以找到所在语法树

    //->>>跳转
    //函数=doc.LastRead.GetFunction(函数名)
    //函数.GoTo()

    //打开指定的文件
    //EditApp.Open(file) 

    //><<<<<<
    //读取Lisp文件 获取函数列表
    //string[] lines = File.ReadAllLines(file, Encoding.Default);
    //if (lines.Length == 0) return;
    //Read rd = new Read(file, lines);
    //rd.ReadToEnd();
    //funLibs = rd.DocPublicUsers;

    //doc.WaitRead() 等待树刷新

    //加载Lisp文件
    //InvokeArx.ads_queueexpr(string.Format("(vl-load-all \"{0}\")", "debug.vlx"));
}
